package com.gxa.modules.service.operationsservice.impl;

import com.gxa.common.R;
import com.gxa.modules.mapper.operationsmapper.CockpitMapper;
import com.gxa.modules.pojo.Tenant;
import com.gxa.modules.service.operationsservice.CockpitService;
import com.gxa.modules.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.logging.SimpleFormatter;

@Service
public class CockpitServiceImpl implements CockpitService {

    @Autowired(required = false)
    CockpitMapper cockpitMapper;

    @Override
    public R selectVisitor() {
        R r = new R();
        List<VisitorInfo> visitorInfos = cockpitMapper.selectVistor();
        int count=0;
        for(int i=0;i<visitorInfos.size();i++)
        {
            visitorInfos.get(i).setNumber(visitorInfos.get(i).getNumber()*new Random().nextInt(15)+15);
            count = count+visitorInfos.get(i).getNumber();
        }
        for(int i=0;i<visitorInfos.size();i++)
        {
            visitorInfos.get(i).setTotal(count);
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(0);
            int num = visitorInfos.get(i).getNumber();
            int total = visitorInfos.get(i).getTotal();
            System.out.println(num+" total+"+total);
            String percent = numberFormat.format((double) num / (double) total * 100);
            String percentstr = percent+"%";
            System.out.println(percentstr);
            visitorInfos.get(i).setPercent(percentstr);
        }
        r.setCode(200);
        r.setMsg("访客监测情况查询成功");
        r.setData(visitorInfos);
        return r;
    }

    @Override
    public R selectPropertyFee() {
        R r= new R();
        List<PropertyFee> list = cockpitMapper.selectPropertyFee();
        int day = 10;
        for(int i=0;i< 7;i++)
        {
            list.get(i).setMonth("11/"+ (day++ +""));
        }
        r.setCode(200);
        r.setMsg("物业费情况");
        r.setData(list);
        return r;
    }

    @Override
    public R selectIncome() {
        R r = new R();
        int totalIncome = 0;
        List<Income> list = new ArrayList<>();
        for(int i=0;i<6;i++)
        {
            Random random = new Random();
            int number = random.nextInt(200);
            Income income = new Income();
            income.setIncome(number+1000);
            income.setDate((i+1)+"月");
            list.add(income);
        }
        for(int i=0;i<6;i++)
        {
            totalIncome= totalIncome + list.get(i).getIncome();
        }
        r.setCode(200);
        r.setTotalIncome(totalIncome);
        r.setData(list);
        r.setMsg("资金收入");
        return r;
    }

    @Override
    public R selectEquipmentMonitor() {
        R r = new R();
        List<Alarm> list = new ArrayList<>();
        int day=20;
        for(int i=0;i<7;i++)
        {
            Random random = new Random();
            int number = random.nextInt(1000);
            Alarm alarm = new Alarm();
            alarm.setFailureCount(number);
            alarm.setDate("11/"+ (day++ +""));
            list.add(alarm);
        }
        r.setCode(200);
        r.setMsg("设备故障监测");
        r.setData(list);
        return r;
    }

    @Override
    public R selectFlow() {
        R r = new R();
        List<Flow> list = new ArrayList<>();
        for(int i=1;i<13;i++)
        {
            Flow flow = new Flow();
            flow.setNumber(new Random().nextInt(80));
            flow.setMonth(i+"月");
            list.add(flow);
        }
        r.setCode(200);
        r.setMsg("人员流量统计");
        r.setData(list);
        return r;
    }

    @Override
    public R selectTenant() {
        R r= new R();
        List<TenantVo> list = new ArrayList<>();
        int day=20;
        for(int i=1;i<=7;i++)
        {
            TenantVo tenantVo = new TenantVo();
            tenantVo.setMoney(new Random().nextInt(200)+800);
            tenantVo.setDate("11"+"/"+(day++ + ""));
            list.add(tenantVo);
        }
        r.setCode(200);
        r.setMsg("租金收入情况");
        r.setData(list);
        return r;
    }

    @Override
    public R selectEarnings() {
        R r=new R();
        List<Earnings> list = new ArrayList<>();
        int day=1;
        List<PropertyFee> list1 = cockpitMapper.selectPropertyFee();
        for(int i=0;i<=6;i++)
        {
            Earnings earnings = new Earnings();
            earnings.setPropertyFee(list1.get(i).getPropertyFee().toString());
            earnings.setUtilities(new Random().nextInt(200)+10);
            earnings.setCarBill(new Random().nextInt(200)+10);
            earnings.setDate(day + "");
            day=day+5;
            list.add(earnings);
        }
        r.setCode(200);
        r.setMsg("当月收益汇总");
        r.setData(list);
        return r;
    }

    @Override
    public R selectRentRemind() throws ParseException {
        R r = new R();
        List<RentRemind> list = cockpitMapper.selectRentRemind();
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
        for(int i=0;i<list.size();i++)
        {
           if(list.get(i).getOverTime().compareTo(simpleFormat.parse("2022-05-10"))>0){
               list.get(i).setProperty("逾期");
           }
        }
        r.setCode(200);
        r.setMsg("新增租赁提醒");
        r.setData(list);
        return r;
    }

}
